const upload = require('./oss/upload.js')

Component({
   properties: {
      multiple: {
         type: Number,
         value: 1,
      }
   },

   data: {
      image: {
         temp: [],
         urls: [],
         progress: [],
         complete: [],
         overflow: false
      }
   },

   attached() {
      this.setData({
         'image.progress': Array(this.properties.multiple).fill(0),
         'image.complete': Array(this.properties.multiple).fill(false),
         'image.fail': Array(this.properties.multiple).fill(false),
      })
   },

   methods: {
      imageSelect(e) {
         let {
            image
         } = this.data
         wx.chooseImage({
            count: this.properties.multiple - image.temp.length,
            success: res => {
               /// 保存临时文件
               image.temp = image.temp.concat(res.tempFilePaths)
               this.setData({
                  image,
                  'image.overflow': image.temp.length >= this.properties.multiple
               })

               // console.log(image)
               //// 开始上传
               this.imageUpload(image.temp)
            },
         })
      },

      imageUpload(temp) {
         temp.forEach((filePath, i) => {
            let {
               progress,
               complete
            } = this.data.image
            if (!complete[i] && progress[i] === 0) {
               upload({
                  filePath,
                  uploading: res => {
                     this.setData({
                        [`image.progress[${i}]`]: res.progress
                     })
                  },
                  success: res => {
                     console.log(res)
                     this.setData({
                        [`image.urls[${i}]`]: res,
                        [`image.complete[${i}]`]: true
                     })
                  },
                  fail: err => {
                     console.log(err)
                     wx.showToast({
                        title: '上传失败'
                     })
                     this.setData({
                        [`image.fail[${i}]`]: true
                     })
                  }
               })
            }
         })
      },

      imagePreview(e) {
         wx.previewImage({
            current: e.currentTarget.dataset.src,
            urls: this.data.image.temp
         })
      },

      imageDelete(e) {
         let i = parseInt(e.currentTarget.dataset.index)

         let {
            temp,
            urls
         } = this.data.image
         if (temp.length > 0) {
            temp[i] && temp.splice(i, 1)
            urls[i] && urls.splice(i, 1)
            this.setData({
               'image.temp': temp,
               'image.urls': urls,
               [`image.progress[${i}]`]: 0,
               [`image.complete[${i}]`]: false,
               [`image.fail[${i}]`]: false,
               'image.overflow': false
            })
            console.log(this.data.image)
         }
      },

      imageClear() {
         const image = {
            temp: [],
            urls: [],
            progress: Array(this.properties.multiple).fill(0),
            complete: Array(this.properties.multiple).fill(false),
            fail: Array(this.properties.multiple).fill(false),
            overflow: false
         }
         this.setData({
            image
         })
      }

   }
})